#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using WaveMaster.Holidays.Club.Domain;
using WaveMaster.Holidays.Club.Service;
using System.Collections;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class UserControlTransferPassenger : UserControl
    {
        User m_current_user;
        MainForm m_mainForm;
        SeaPortService m_seaPortService;
        CheckInService m_bookingService;
        TripService m_tripService;
        VesselService m_vesselService;

        public UserControlTransferPassenger(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            m_current_user = currentUser;
            m_mainForm = mainForm;
            this.Name = name;
            m_tripService = new TripService();// (TripService)ServiceFactory.Instance().GetService(ServiceFactory.TRIP_SERVICE);
            m_seaPortService = new SeaPortService();// (SeaPortService)ServiceFactory.Instance().GetService(ServiceFactory.SEAPORT_SERVICE);
            m_vesselService = new VesselService();// (VesselService)ServiceFactory.Instance().GetService(ServiceFactory.VESSEL_SERVICE);
            m_bookingService = new CheckInService();// (CheckInService)ServiceFactory.Instance().GetService(ServiceFactory.BOOKING_SERVICE);

           // timerClearResult.Tick += new EventHandler(timerClearResult_Tick);
            timerUpdateRefresh.Tick += new EventHandler(timerUpdateRefresh_Tick);
            //timerExecuteBoardingPass.Tick += new EventHandler(timerExecuteBoardingPass_Tick);

           // timerExecuteBoardingPass.Interval = 100;

            initComboDataSource();
            initButtonEvent();
            initButtonPermission();
            m_current_user = currentUser;
            m_mainForm = mainForm;
            this.Name = name;
            UpdatePassengerStatus();

            timerUpdateRefresh.Start();
            dateTimePickerBoardingDateNew.MinDate = dateTimePickerBoardingDate.Value;
            
        }

        void timerExecuteBoardingPass_Tick(object sender, EventArgs e)
        {
            //string bp = textBoxBoardingPass.Text;
            //Boarding();
            //timerExecuteBoardingPass.Stop();
        }

        void timerUpdateRefresh_Tick(object sender, EventArgs e)
        {
            UpdatePassengerStatus();
           // comboBoxDepartureTrip_SelectedIndexChanged(sender, e);
           // comboBoxDepartureTripNew_SelectedIndexChanged(sender, e);
        }

        private void initButtonPermission()
        {
          //  textBoxBoardingPass.Enabled = m_current_user.RoleDetails(this.Name).NewFlag;
            buttonBOarding.Enabled = m_current_user.RoleDetails(this.Name).NewFlag || m_current_user.RoleDetails(this.Name).UpdateFlag;
            menuItem1.Enabled = false;// m_current_user.RoleDetails(this.Name).NewFlag;
            menuItem2.Enabled = false;//m_current_user.RoleDetails(this.Name).NewFlag; 
        }

        void timerClearResult_Tick(object sender, EventArgs e)
        {
           // m_txtBoardingPassKey = Keys.A;
            //textBoxMessage.Text = "";
            //textBoxMessage.BackColor = System.Drawing.Color.Transparent;
            timerClearResult.Stop();
        }

        private void initButtonEvent()
        {
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
           // toolBarButtonPrintManifest.Click += new EventHandler(toolBarButtonPrintManifest_Click);
        }

        //void toolBarButtonPrintManifest_Click(object sender, EventArgs e)
        //{
        //    Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
        //    if (trip == null) return;
        //    PrintManifestForm pm = new PrintManifestForm(trip);
        //    pm.ShowDialog();
        //}

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            //comboBoxDepartureSeaPort_SelectedIndexChanged(sender, e);
            UpdatePassengerStatus();
            comboBoxDepartureTrip_SelectedIndexChanged(sender, e);
            comboBoxDepartureTripNew_SelectedIndexChanged(sender, e);
            //m_txtBoardingPassKey = Keys.A;
            //textBoxBoardingPass.Text = "";
            //textBoxBoardingPass.SelectAll();
        }

        private void UpdatePassengerStatus()
        {
            Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
            if (trip == null) return;
            trip.m_vessel = m_vesselService.GetVesselByID(trip.m_vessel.m_vesselID);

            int capacity = trip.m_vessel.m_capacity;
            int checkIn = m_bookingService.GetPassengerCountByStatusAndTrip(PassengerStatus.CheckIn, trip);
            int boarded = m_bookingService.GetPassengerCountByStatusAndTrip(PassengerStatus.Boarding, trip);
            int unboarded = m_bookingService.GetPassengerCountByStatusAndTrip(PassengerStatus.UnBoarded, trip);
            int available = capacity - (checkIn + boarded + unboarded);

            labelCapacity.Text = capacity.ToString();
            labelCheckin.Text = checkIn.ToString();
            labelBoarded.Text = boarded.ToString();
            labelUnboarded.Text = unboarded.ToString();

            Trip tripNew = (Trip)comboBoxDepartureTripNew.SelectedValue;
            if (tripNew == null) return;
            tripNew.m_vessel = m_vesselService.GetVesselByID(tripNew.m_vessel.m_vesselID);

            int capacityNew = tripNew.m_vessel.m_capacity;
            int checkInNew = m_bookingService.GetPassengerCountByStatusAndTrip(PassengerStatus.CheckIn, tripNew);
            int boardedNew = m_bookingService.GetPassengerCountByStatusAndTrip(PassengerStatus.Boarding, tripNew);
            int unboardedNew = m_bookingService.GetPassengerCountByStatusAndTrip(PassengerStatus.UnBoarded, tripNew);
            int availableNew = capacity - (checkIn + boarded + unboarded);

            labelCapacityNew.Text = capacityNew.ToString();
            labelCheckinNew.Text = checkInNew.ToString();
            labelBoardedNew.Text = boardedNew.ToString();
            labelUnboardedNew.Text = unboardedNew.ToString();
        }

        private void initComboDataSource()
        {
            comboBoxDepartureSeaPort.DataSource = m_current_user.UserType == UserType.Administrators ? m_seaPortService.LoadAllSeaPort() : m_seaPortService.LoadAllActiveSeaPort();
            comboBoxDepartureSeaPortNew.DataSource = m_current_user.UserType == UserType.Administrators ? m_seaPortService.LoadAllSeaPort() : m_seaPortService.LoadAllActiveSeaPort();
        }

        private void buttonBOarding_ClickConfirmation(object sender, EventArgs e)
        {
            MessageBox.Show("Are you sure to transfer this passanger?", "Confirmation", MessageBoxButtons.YesNo, new EventHandler(buttonBOarding_Click));
        }

        private void buttonBOarding_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBoxWindow ms = (MessageBoxWindow)sender;
                if (ms.DialogResult == DialogResult.No) return;
                if (listViewCheckIn.SelectedItems.Count == 0) return;
                if (comboBoxDepartureTripNew.SelectedItem == null) return;
                Trip newTrip = (Trip)comboBoxDepartureTripNew.SelectedItem;
                foreach (ListViewItem itm in listViewCheckIn.SelectedItems)
                {
                    Passenger psg = (Passenger)itm.Tag;
                    m_bookingService.TransferPassenger(psg, newTrip);
                    listViewCheckIn.Items.Remove(itm);
                }
                comboBoxDepartureTripNew_SelectedIndexChanged(sender, e);
                UpdatePassengerStatus();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }
      
        private void clearErrorProvider()
        {
         //   errorProviderBoarding.SetError(comboBoxDepartureTrip, "");
        }

        private void comboBoxDepartureSeaPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxDepartureTrip.DataSource = null;
            if (comboBoxDepartureSeaPort.SelectedValue == null) return;
            SeaPort port = (SeaPort)comboBoxDepartureSeaPort.SelectedValue;
            comboBoxDepartureTrip.DataSource = m_tripService.LoadAllTripByDateAndSeaPort(dateTimePickerBoardingDate.Value, port.m_seaPortID);
        }

        private void comboBoxDepartureTrip_SelectedIndexChanged(object sender, EventArgs e)
        {
            listViewCheckIn.Items.Clear();
            comboBoxDepartureTripNew.DataSource = null;
            listViewBoardingNew.Items.Clear();
            
            Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
            if (trip == null) return;

            IList passCheckIn =
               m_current_user.UserType == UserType.Administrators ?
                m_bookingService.GetPassengerByStatusAndTrip(PassengerStatus.CheckIn, trip)
                : m_bookingService.GetPassengerByStatusAndTrip(PassengerStatus.CheckIn, trip, m_current_user.TravelAgent);

            foreach (Passenger p in passCheckIn)
            {
                ListViewItem item = listViewCheckIn.Items.Add(p.m_boardingPassCode);
                item.SubItems.Add(p.m_passportNo);
                item.SubItems.Add(p.m_name);
                item.Tag = p;
            }

            IList passUnBoarded =
               m_current_user.UserType == UserType.Administrators ?
                m_bookingService.GetPassengerByStatusAndTrip(PassengerStatus.UnBoarded, trip)
                : m_bookingService.GetPassengerByStatusAndTrip(PassengerStatus.UnBoarded, trip, m_current_user.TravelAgent);
            foreach (Passenger p in passUnBoarded)
            {
                ListViewItem item = listViewCheckIn.Items.Add(p.m_boardingPassCode);
                item.SubItems.Add(p.m_passportNo);
                item.SubItems.Add(p.m_name);
                item.Tag = p;
            }

            comboBoxDepartureSeaPortNew_SelectedIndexChanged(sender, e);
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            //if (listViewCheckIn.SelectedItem == null) return;
            //Passenger pass = (Passenger)listViewCheckIn.SelectedItem.Tag;
            //if (pass == null) return;
            //Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
            //m_bookingService.UpdatePassangerStatus(pass, PassengerStatus.Boarding);
            //comboBoxDepartureTrip_SelectedIndexChanged(sender, e);
            //UpdatePassengerStatus();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //if (listViewBoardingNew.SelectedItem == null) return;
            //Passenger pass = (Passenger)listViewBoardingNew.SelectedItem.Tag;
            //if (pass == null) return;
            //Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
            //m_bookingService.UpdatePassangerStatus(pass, PassengerStatus.UnBoarded);
            //comboBoxDepartureTrip_SelectedIndexChanged(sender, e);
            //UpdatePassengerStatus();
        }

        //private void textBoxBoardingPass_TextChanged(object sender, EventArgs e)
        //{
        //    //if (m_txtBoardingPassKey == Keys.Enter)
        //    //{
        //    //    Boarding();
        //    //}
        //    //m_txtBoardingPassKey = Keys.A;
        //}

        //Keys m_txtBoardingPassKey = Keys.A;

        //private void textBoxBoardingPass_KeyDown(object objSender, KeyEventArgs objArgs)
        //{
        //    //m_txtBoardingPassKey = objArgs.KeyData;
        //    if (objArgs.KeyData == Keys.Enter)
        //    {
        //        timerExecuteBoardingPass.Start();
        //        //Boarding();
        //    }
        //   // m_txtBoardingPassKey = Keys.A;
        //}

        private void dateTimePickerBoardingDate_ValueChangedQueued(object sender, EventArgs e)
        {
            comboBoxDepartureSeaPort_SelectedIndexChanged(sender, e);
            
            dateTimePickerBoardingDateNew.MinDate = dateTimePickerBoardingDate.Value;
            comboBoxDepartureTripNew.DataSource = null;
            listViewBoardingNew.Items.Clear();
            comboBoxDepartureSeaPortNew_SelectedIndexChanged(sender, e);
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            //Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
            //if (trip == null) return;
            //PrintManifestForm pm = new PrintManifestForm(trip, false, m_current_user);
            //pm.ShowDialog();
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            //Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
            //if (trip == null) return;
            //PrintManifestForm pm = new PrintManifestForm(trip, true, m_current_user);
            //pm.ShowDialog();
        }

        private void textBoxBoardingPass_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (e.KeyChar == Convert.ToChar(Keys.Enter))
            //{
            //    timerExecuteBoardingPass.Start();
            //}
        }

        private void dateTimePickerBoardingDateNew_ValueChanged(object sender, EventArgs e)
        {
            comboBoxDepartureSeaPortNew_SelectedIndexChanged(sender, e);
        }

        private void comboBoxDepartureSeaPortNew_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxDepartureTripNew.DataSource = null;
            if (comboBoxDepartureSeaPortNew.SelectedValue == null) return;
            if (comboBoxDepartureTrip.SelectedValue == null) return;
            Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue; 
            SeaPort port = (SeaPort)comboBoxDepartureSeaPortNew.SelectedValue;
            comboBoxDepartureTripNew.DataSource = m_tripService.LoadAllTripByDateAndSeaPort(dateTimePickerBoardingDateNew.Value, port.m_seaPortID, trip.m_departureTime);
        }

        private void comboBoxDepartureTripNew_SelectedIndexChanged(object sender, EventArgs e)
        {
            listViewBoardingNew.Items.Clear();

            Trip trip = (Trip)comboBoxDepartureTripNew.SelectedValue;
            if (trip == null) return;

            IList passCheckIn =
               m_current_user.UserType == UserType.Administrators ?
                m_bookingService.GetPassengerByStatusAndTrip(PassengerStatus.CheckIn, trip)
                : m_bookingService.GetPassengerByStatusAndTrip(PassengerStatus.CheckIn, trip, m_current_user.TravelAgent);

            foreach (Passenger p in passCheckIn)
            {
                ListViewItem item = listViewBoardingNew.Items.Add(p.m_boardingPassCode);
                item.SubItems.Add(p.m_passportNo);
                item.SubItems.Add(p.m_name);
                item.Tag = p;
            }

            IList passUnBoarded =
              m_current_user.UserType == UserType.Administrators ?
               m_bookingService.GetPassengerByStatusAndTrip(PassengerStatus.UnBoarded, trip)
               : m_bookingService.GetPassengerByStatusAndTrip(PassengerStatus.UnBoarded, trip, m_current_user.TravelAgent);
            foreach (Passenger p in passUnBoarded)
            {
                ListViewItem item = listViewBoardingNew.Items.Add(p.m_boardingPassCode);
                item.SubItems.Add(p.m_passportNo);
                item.SubItems.Add(p.m_name);
                item.Tag = p;
            }
        }
    }
}